package com.zgjkhis.modules.system.rest;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zgjkhis.domain.PageResult;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.charges.service.PrescriptionRefundService;
import com.zgjkhis.modules.security.security.LoginUserUtil;
import com.zgjkhis.modules.statistics.resp.DrugRefundResp;
import com.zgjkhis.modules.system.domain.Doctor;
import com.zgjkhis.modules.system.domain.Info;
import com.zgjkhis.modules.system.domain.vo.*;
import com.zgjkhis.modules.system.req.RefundReq;
import com.zgjkhis.modules.system.service.DoctorService;
import com.zgjkhis.modules.system.service.InfoDrugService;
import com.zgjkhis.modules.system.service.InfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName DrugStatisticsController
 * @Deacription TODO
 * @Author yzq
 * @Date 2021/7/12 14:48
 * @Version 1.0
 **/
@Api(tags = "药品统计")
@RestController
@RequestMapping("/drugStatistics")
public class DrugStatisticsController {

    private final InfoDrugService infoDrugService;

    private final InfoService infoService;

    private final DoctorService doctorService;

    private final PrescriptionRefundService prescriptionRefundService;


    public DrugStatisticsController(InfoDrugService infoDrugService,
                                    InfoService infoService, DoctorService doctorService, PrescriptionRefundService prescriptionRefundService) {
        this.infoDrugService = infoDrugService;
        this.infoService = infoService;
        this.doctorService = doctorService;
        this.prescriptionRefundService = prescriptionRefundService;
    }

    @PostMapping(value = "/sales")
    @ApiOperation("销售统计")
    public ResponseEntity<List<DrugSaleVo>> sales(String startTime, String endTime){

        String pharmacyId = LoginUserUtil.getPharmId();
        List<DrugSaleVo> drugSaleVo=infoDrugService.drugSale(pharmacyId,startTime + " 00:00:00", endTime + " 23:59:59");

        return new ResponseEntity<>(drugSaleVo, HttpStatus.OK);
    }


    @PostMapping(value = "/infoSalesDoc")
    @ApiOperation("医生处方")
    public ResponseEntity<InfoHomeSaleVo> infoSalesDoc(String startTime, String endTime,String docName,String historyType){
        String pharmacyId= LoginUserUtil.getPharmId();
        startTime += " 00:00:00";
        endTime += " 23:59:59";
        List<InfoSaleVo> infoSaleVos=infoService.infoStatistics(pharmacyId, startTime, endTime,docName,historyType);
        InfoHomeSaleVo infoHomeSaleVo=new InfoHomeSaleVo();
        infoHomeSaleVo.setInfoSaleVoList(infoSaleVos);
        long docNum = doctorService.count(Wrappers.<Doctor>query().lambda().
                eq(Doctor::getPharmacyId, pharmacyId).eq(Doctor::getStatus, 0));
        infoHomeSaleVo.setDocNum(docNum);
        Integer userNum=infoService.list(Wrappers.<Info>lambdaQuery()
                .and(wrapper -> wrapper.eq(Info::getPayStatus,1).or().eq(Info::getPayStatus,2)).eq(Info::getPharmacyId,pharmacyId)).stream().map(entity -> entity.getPatientId()).distinct().collect(Collectors.toList()).size();
        infoHomeSaleVo.setUserNum(userNum);
        return new ResponseEntity<>(infoHomeSaleVo, HttpStatus.OK);
    }


    @Log("导出医生处方")
    @ApiOperation("导出医生处方")
    @GetMapping(value = "/outInfoSalesDoc")
    public void outInfoSalesDoc(String startTime, String endTime,String docName,String historyType, HttpServletResponse response) throws IOException {
        String pharmacyId= LoginUserUtil.getPharmId();
        infoService.outInfoSalesDoc(pharmacyId, startTime, endTime,docName,historyType,response);
    }



    @PostMapping(value = "/infoSalesDate")
    @ApiOperation("销售统计")
    public ResponseEntity<InfoMarketHomeSaleVo> infoSalesDate(String startTime, String endTime,String historyType){
        String pharmacyId= LoginUserUtil.getPharmId();
        InfoMarketHomeSaleVo infoSaleVos=infoService.infoSalesDate(pharmacyId,startTime + " 00:00:00", endTime + " 23:59:59", historyType);
        return new ResponseEntity<>(infoSaleVos, HttpStatus.OK);
    }

    @Log("导出销售统计")
    @ApiOperation("导出销售统计")
    @GetMapping(value = "/outInfoSalesDate")
    public void outInfoSalesDate(String startTime, String endTime,String docName,String historyType, HttpServletResponse response) throws IOException {
        String pharmacyId= LoginUserUtil.getPharmId();
        infoService.outInfoSalesDate(pharmacyId, startTime, endTime,docName,historyType,response);
    }

    @PostMapping(value = "/infoSalesDateTop")
    @ApiOperation("销售统计顶部")
    public ResponseEntity<InfoMarketTopSaleVo> infoSalesDateTop(){
        String pharmacyId= LoginUserUtil.getPharmId();

        InfoMarketTopSaleVo infoMarketTopSaleVo=infoService.infoMarketTop(pharmacyId);
        infoMarketTopSaleVo.setPayFee(infoMarketTopSaleVo.getAllFee().subtract(infoMarketTopSaleVo.getCancelFee()));

        return new ResponseEntity<>(infoMarketTopSaleVo, HttpStatus.OK);
    }

    @PostMapping(value = "/initialization")
    @ApiOperation("type:0 初始化")
    public void dayPigeonhole(String date,String type) throws ParseException {

        if("0".equals(type)){
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
            String newDate= DateUtil.formatDate(new Date());
            Date d1=sdf.parse(date);
            Date d2=sdf.parse(newDate);
            Date tmp=d1;
            Calendar dd=Calendar.getInstance();
            dd.setTime(d1);
            while (tmp.getTime()<d2.getTime()){
                tmp=dd.getTime();
                infoService.dayPigeonhole(sdf.format(tmp));
                dd.add(Calendar.DAY_OF_MONTH,1);
            }
        }else if("1".equals(type)){
            infoService.dayPigeonhole(date);
        }

    }

    @PostMapping(value = "/refundList")
    @ApiOperation("药品退款记录")
    public ResponseEntity<PageResult<DrugRefundResp>> refundList(RefundReq refundReq, Pageable pageable){
        String pharmacyId = LoginUserUtil.getPharmId();
        refundReq.setPharmacyId(pharmacyId);
        return new ResponseEntity<>(infoDrugService.refundList(refundReq, pageable), HttpStatus.OK);
    }

    @PostMapping(value = "/outRefundList")
    @ApiOperation("导出药品退款记录")
    public void outRefundList(RefundReq refundReq, HttpServletResponse response) throws IOException {
        String pharmacyId= LoginUserUtil.getPharmId();
        refundReq.setPharmacyId(pharmacyId);
        infoDrugService.outRefundList(refundReq,response);
    }

}
